`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date:    22:12:14 04/01/2014 
// Design Name: 
// Module Name:    movement_ctrl 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
module movement_ctrl(up,down,left,right,a,
							bullet,pheonix,an_enemy_sprite,
							VS,		// vertical sync
							rst,clk,pixel_clk,
							Xchar,Ychar,Xproj,Yproj);
							
	// --- i/o --------------------------------------------						
	input rst,clk,pixel_clk,bullet,pheonix,an_enemy_sprite;
	input up,down,left,right,a;
	input VS;
	output reg [9:0] Xchar,Ychar,Xproj,Yproj;

//	`define b_delay_max 150000
//	`define fire_delay_max 100000000
	parameter move_delay_max = 24;

	// --- registers --------------------------------------
	reg [18:0] b_delay;
	reg [8:0] move_delay;
	reg [30:0] fire_delay;
	reg [9:0] X,Y,Xb,Yb;
	reg bullet_ready;
	
	//reg [32:0] fire_delay_max;
	//reg [30:0] move_delay_max;

	initial begin
		X <= 10'd200;
		Y <= 10'd400;
		Yb <= 10'd700;
		b_delay <= 10'd0;
		fire_delay <= 10'd0;
		bullet_ready <= 10'b1;
		move_delay <= 10'd0;
		//fire_delay_max <= 100000000;
		//move_delay_max <= 1500;
	end
	
	// --- flags --------------------------------------------	
	always @ (posedge clk) begin
		if (rst) bullet_ready <= 1'b1;
		else if (a) bullet_ready <= 0;
		else if (fire_delay == 80000000) bullet_ready <=1;
	end
	
//	always @ (posedge clk) begin
//		if (rst) fire_delay_max <= 100000000;
//		else if (flags[0] == 1'b1) fire_delay_max <= fire_delay_max - 30000000;
//	end
//	
//	always @ (posedge clk) begin
//		if (rst) move_delay_max <= 1500;
//		else if (flags[1] == 1'b1) move_delay_max <= move_delay_max - 200;
//	end
		
	// --- delay timers -------------------------------------
	always @ (posedge clk) begin
		if (rst) fire_delay <= 0;
		else if (bullet_ready) fire_delay <= 0;
		else fire_delay <= fire_delay + 1'b1;
	end
	
	always @ (posedge clk) begin
		if (rst) b_delay <= 0;
		else if (b_delay == 150000) b_delay <= 0;
		else b_delay <= b_delay + 1'b1;
	end

	always @ (posedge clk) begin
		if (rst) move_delay <= 0;
		else if (move_delay > move_delay_max) move_delay <= 0;
		else move_delay <= move_delay + 1'b1;
	end
	
	// --- bullet control -----------------------------------
	always @ (posedge clk) begin
		if(rst) 	Xb <= 500;
		if(a && bullet_ready ) begin
			Xb <= X+ 9'd8;
		end
	end
	
	always @ (posedge clk) begin
		if(rst) 	Yb <= 9'd500;			// somewhere off screen
		else if(a && bullet_ready ) begin
			Yb <= Y - 10'd32;				// bullet appears at character location
		end
		else if (Yb == 1 || (bullet & an_enemy_sprite) || bullet & pheonix) begin
			Yb <= 9'd500; 					// make it disappear
		end
		else if(Yb<480) begin
			if (b_delay == 150000 ) Yb <= Yb - 1'b1;	// move the bullet
		end
	end	
	
	// --- character control --------------------------------
	always @ (posedge clk) begin
		if (rst) Y <= 400;
		else if(up && Y>64 ) begin
			if (move_delay == move_delay_max) Y <= Y - 1'b1;		// boundary control
		end
		else if(down && Y<416 ) begin
			if (move_delay == move_delay_max) Y <= Y+ 1'b1;
		end
	end

	always @ (posedge clk) begin
		if(rst) 	X <= 200;
		else if(left && X>32 ) begin
			if (move_delay == move_delay_max) X <= X - 1'b1;	// boundary control
		end
		else if (right && X<448 ) begin
			if (move_delay == move_delay_max) X <= X + 1'b1;
		end
	end
	
	
	// -- update registers with vsync ------------------------

	always @ (posedge pixel_clk) begin
		if(!VS) Xchar <= X;
	end
	
	always @ (posedge pixel_clk) begin
		if(!VS) Ychar <= Y;
	end

	always @ (posedge pixel_clk) begin
		if(!VS) Xproj <= Xb;
	end

	always @ (posedge pixel_clk) begin
		if(!VS) Yproj <= Yb;
	end

endmodule
